package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Dto.SetmealDto;
import com.itheima.reggie.common.R;

import com.itheima.reggie.pojo.Setmeal;

import com.itheima.reggie.service.SetdmealService;
import com.itheima.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetdmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize, String name) {
        //分页构造器对象
        Page<Setmeal> pages = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据name进行模糊查询
        queryWrapper.like(name != null, Setmeal::getName, name);
        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //page方法
       setmealService.page(pages, queryWrapper);
        return R.success(pages);
    }

    @DeleteMapping
    public R<String> del(@RequestParam List<Long> ids){

        setmealService.removeByIds(ids);

        return R.success("删除成功");
    }


    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithD(setmealDto);

        return R.success("保存成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> Updata(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.SelectById(id);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> UpdataWithS(@RequestBody SetmealDto setmealDto){

        setmealService.UpdataWithS(setmealDto);

        return R.success("保存成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>>  selByid(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,
                Setmeal::getCategoryId,setmeal.getCategoryId());

        queryWrapper.eq(setmeal.getStatus() != null ,
                Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}